<template>
  <a-modal
    title="通过文件夹新增"
    :width="720"
    :visible="visible"
    :confirmLoading="confirmLoading"
    :maskClosable="false"
    :destroyOnClose="true"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item label="文件夹" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-select
            v-decorator="['dir', { initialValue: '' ,rules: [{required: true}]}]">
            <a-select-option
              v-for="(item, index) in dirList"
              :key="index"
              :value="item.value"
            >{{ item.label }}</a-select-option>
          </a-select>
        </a-form-item>
        <a-form-item label="名称" :labelCol="labelCol" :wrapperCol="wrapperCol">
          <a-input
            v-decorator="['name']"
          />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>

import * as Api from '@/api/book/file'
export default {
  components: {
  },
  data () {
    return {
      // 标签布局属性
      labelCol: { span: 7 },
      // 输入框布局属性
      wrapperCol: {
        span: 13
      },
      // modal(对话框)是否可见
      visible: false,
      // modal(对话框)确定按钮 loading
      confirmLoading: false,
      // 当前表单元素
      form: this.$form.createForm(this),
      // 当前记录
      record: {},
      dirList:[],
    }
  },
  methods: {

    /**
     * 显示对话框
     */
    open () {
      // 显示窗口
      this.visible = true
      this.getDirList()
    },
    getDirList(){
      Api.dirList().then(res=>{
        this.dirList = res.data
      })
    },

    /**
     * 确认按钮
     */
    handleSubmit (e) {
      e.preventDefault()
      // 表单验证
      const { form: { validateFields } } = this
      validateFields((errors, values) => {
        // 提交到后端api
        if (!errors) {
          this.confirmLoading=true
          Api.add(values).then(res=>{
            // 显示成功
            this.$message.success(res.message, 1.5)
            // 关闭对话框事件
            this.handleCancel()
            // 通知父端组件提交完成了
            this.$emit('handleSubmit')
          }).finally(()=>{
            this.confirmLoading = false
          })
        }
      })
    },

    /**
     * 关闭对话框事件
     */
    handleCancel () {
      this.visible = false
    },
  }
}
</script>
